#!/bin/bash
set -e

# 安装Python虚拟环境依赖
apt-get update && apt-get install -y python3-venv

# 创建并激活Python虚拟环境
python3 -m venv /usr/local/firealam/.venv

# 配置pip默认使用阿里云镜像
mkdir -p /usr/local/firealam/.venv/pip
cat > /usr/local/firealam/.venv/pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
EOF

. /usr/local/firealam/.venv/bin/activate
pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip3 install -r /usr/local/firealam/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
deactivate

# 安装服务文件
cp /usr/local/firealam/firealam.service /etc/systemd/system/

# 设置服务
systemctl daemon-reload
systemctl enable firealam.service
systemctl start firealam.service

# 设置权限
chmod +x /usr/local/firealam/firealam.py
chown -R pi:pi /usr/local/firealam

# 安装桌面快捷方式
cp /usr/local/firealam/firealam.desktop /usr/share/applications/
chmod 644 /usr/share/applications/firealam.desktop
update-desktop-database /usr/share/applications/

echo "火灾报警系统安装完成"

exit 0